/*
 * @{#} DateUtil.java Created on 2011-1-20 上午11:16:39
 *
 * CopyRight: (C) 2006 WASU.
*/
package com.zhiyume.service.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil
{

    public DateUtil()
    {
    }

    public static String shortDate(Date aDate)
    {
        if(aDate == null)
        {
            return "";
        } else
        {
            formatter = new SimpleDateFormat("yyyy-MM-dd");
            return formatter.format(aDate);
        }
    }

    public static String mailDate(Date aDate)
    {
        if(aDate == null)
        {
            return "";
        } else
        {
            formatter = new SimpleDateFormat("yyyyMMddHHmm");
            return formatter.format(aDate);
        }
    }

    public static String longDate(Date aDate)
    {
        if(aDate == null)
        {
            return "";
        } else
        {
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter.format(aDate);
        }
    }

    public static String shortDateGB(Date aDate)
    {
        if(aDate == null)
        {
            return "";
        } else
        {
            formatter = new SimpleDateFormat("yyyy'\u5E74'MM'\u6708'dd'\u65E5'");
            return formatter.format(aDate);
        }
    }

    public static String longDateGB(Date aDate)
    {
        if(aDate == null)
        {
            return "";
        } else
        {
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter.format(aDate);
        }
    }

    public static String formatDate(Date aDate, String formatStr)
    {
        if(aDate == null)
        {
            return "";
        } else
        {
            formatter = new SimpleDateFormat(formatStr);
            return formatter.format(aDate);
        }
    }

    public static String LDAPDate(Date aDate)
    {
        if(aDate == null)
            return "";
        else
            return formatDate(aDate, "yyyyMMddHHmm'Z'");
    }

    public static Date getDate(String yyyymmdd)
    {
        if(yyyymmdd == null)
        {
            return null;
        } else
        {
            int year = Integer.parseInt(yyyymmdd.substring(0, yyyymmdd.indexOf("-")));
            int month = Integer.parseInt(yyyymmdd.substring(yyyymmdd.indexOf("-") + 1, yyyymmdd.lastIndexOf("-")));
            int day = Integer.parseInt(yyyymmdd.substring(yyyymmdd.lastIndexOf("-") + 1));
            Calendar cal = Calendar.getInstance();
            cal.set(year, month - 1, day);
            return cal.getTime();
        }
    }
    
    
    public static Date parser(String strDate)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }

    public static Date getShortDate(String date)
    {
        Date shortDate = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try
        {
            shortDate = formatter.parse(date);
        }
        catch(Exception e)
        {
            shortDate = null;
        }
        return shortDate;
    }

    public static boolean equals(Date date1, Date date2)
    {
        if(date1 == null && date2 == null)
            return true;
        if(date1 == null && date2 != null)
            return false;
        if(date1 != null && date2 == null)
            return false;
        else
            return date1.equals(date2);
    }

    public static Date tomorrow()
    {
        Calendar calender = Calendar.getInstance();
        calender.roll(6, true);
        return calender.getTime();
    }

    public static Date nextDate(Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.roll(6, 1);
        if(isEndOfYear(date, cal.getTime()))
        {
            cal.roll(1, true);
            cal.roll(6, 1);
        }
        return cal.getTime();
    }

    private static boolean isEndOfYear(Date curDate, Date rollUpDate)
    {
        return curDate.compareTo(rollUpDate) >= 0;
    }

    public static Date yesterday()
    {
        Calendar calender = Calendar.getInstance();
        calender.roll(6, false);
        return calender.getTime();
    }

    private static final String getDateFormat(Date aDate)
    {
        if(aDate == null)
        {
            return null;
        } else
        {
            SimpleDateFormat formatter = new SimpleDateFormat("M/d/yyyy");
            return formatter.format(aDate);
        }
    }

    public static String NVL(Date date)
    {
        if(date == null)
            return "";
        else
            return getDateFormat(date);
    }

    public static Date addDate(Date baseDate, int type, int num)
    {
        Date lastDate = null;
        Calendar cale = Calendar.getInstance();
        cale.setTime(baseDate);
        if(type == 1)
            cale.add(1, num);
        else
        if(type == 2)
            cale.add(2, num);
        else
        if(type == 3)
            cale.add(5, num);
        lastDate = cale.getTime();
        return lastDate;
    }

    public static Date getDate(String strDate, String formatter)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        try {
			return sdf.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    

    public static Date getSysDate()
    {
        return new Date(System.currentTimeMillis());
    }

    public static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy");
    public static final DateFormat FULL_DATE_FORMAT = new SimpleDateFormat("EEE, MMM d, yyyyy hh:mm:ss aa z");
    public static final DateFormat ISO8601_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS z");
    private static SimpleDateFormat formatter;



    /** 日期格式 **/
    public interface DATE_PATTERN {
        String HHMMSS = "HHmmss";
        String HH_MM_SS = "HH:mm:ss";
        String YYYYMMDD = "yyyyMMdd";
        String YYYY_MM_DD = "yyyy-MM-dd";
        String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
        String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    }
    /**
     * 将Date类型转换成String类型
     *
     * @param date
     *            Date对象
     * @return 形如:"yyyy-MM-dd HH:mm:ss"
     */
    public static String date2String(Date date) {
        return date2String(date, DATE_PATTERN.YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 将Date按格式转化成String
     *
     * @param date
     *            Date对象
     * @param pattern
     *            日期类型
     * @return String
     */
    public static String date2String(Date date, String pattern) {
        if (date == null || pattern == null){
            return null;
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 将String类型转换成Date类型
     *
     * @param date
     *            Date对象
     * @return
     */
    public static Date string2Date(String date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN.YYYY_MM_DD_HH_MM_SS);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取当前时间的 yyyy-MM-dd HH:mm:ss 格式的字符串
     * @return
     */
    public static String getNow(){
        return DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss");
    }
    public static String getNowFormatYYYYMDD(){
        return DateUtil.formatDate(new Date(),DATE_PATTERN.YYYY_MM_DD);
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
    }

}
